package pl.pawluk.inz.entities;

import org.springframework.security.core.GrantedAuthority;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

/**
 * Created with IntelliJ IDEA.
 * User: mandp
 * Date: 17.05.13
 * Time: 19:39
 * To change this template use File | Settings | File Templates.
 */
@javax.persistence.IdClass(pl.pawluk.inz.entities.AuthoritiesEntityPK.class)
@javax.persistence.Table(name = "authorities", schema = "public", catalog = "")
@Entity
public class AuthoritiesEntity implements GrantedAuthority {
//    private Long userId;
//
//    @javax.persistence.Column(name = "user_id", nullable = false, insertable = true, updatable = true, length = 19, precision = 0)
//    @Id
//    public Long getUserId() {
//        return userId;
//    }
//
//    public void setUserId(Long userId) {
//        this.userId = userId;
//    }

    private UsersEntity userId;

    @Id
    @ManyToOne
    @javax.persistence.JoinColumn(name = "user_id", referencedColumnName = "id", nullable = false, updatable = false, insertable = false)
    public UsersEntity getUserId() {
        return userId;
    }

    public void setUserId(UsersEntity usersByUserId) {
        this.userId = usersByUserId;
    }

    private String authority;

    @javax.persistence.Column(name = "authority", nullable = false, insertable = true, updatable = true, length = 50, precision = 0)
    @Id
    public String getAuthority() {
        return authority;
    }

    public void setAuthority(String authority) {
        this.authority = authority;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        AuthoritiesEntity that = (AuthoritiesEntity) o;

        if (authority != null ? !authority.equals(that.authority) : that.authority != null) return false;
        if (userId != null ? !userId.equals(that.userId) : that.userId != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = userId != null ? userId.hashCode() : 0;
        result = 31 * result + (authority != null ? authority.hashCode() : 0);
        return result;
    }

}
